/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "sql_operation.h"
#include "connection.h"

namespace node {
    namespace db {

        SqlPreparedRequest::SqlPreparedRequest(int nIndex, SqlStmtParameters* arg) : index_(nIndex), param_(arg)
        {
        }

        SqlPreparedRequest::~SqlPreparedRequest()
        {
            delete param_;
        }

        bool SqlPreparedRequest::execute(Connection* conn)
        {
            Connection::Lock guard(conn);
            return conn->executeStmt(index_, *param_);
        }

        void SqlResultQueue::update()
        {
            ::node::db::IQueryCallback* callback = NULL;
            while (next(callback))
            {
                callback->execute();
                delete callback;
            }
        }

        bool SqlQuery::execute(Connection* conn)
        {
            if (!callback_ || !queue_)
            { return false; }

            Connection::Lock guard(conn);
            callback_->setResult(conn->query(sql_));
            queue_->add(callback_);
            return true;
        }

    }
}